<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<form class="layui-form ">
    <!--<div class="layui-form layuimini-form">-->
    <input type="hidden" name="eid" id="eid" value=""/>

    <div class="layui-form-item">
        <label class="layui-form-label required">设备类型</label>
        <div class="layui-input-block">
            <select name="tid" lay-verify="" lay-filter="test" id="ename">
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">车间</label>
        <div class="layui-input-block">
            <select name="wosId" lay-verify="" lay-filter="test" id="wosId">
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">型号</label>
        <div class="layui-input-block">
            <input type="text" name="model" id="model" lay-verify="required" lay-reqtext="描述不能为空" placeholder=""
                   value=""
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">品牌</label>
        <div class="layui-input-block">
            <input type="text" name="brand" id="brand" lay-verify="required" lay-reqtext="品牌不能为空" placeholder=""
                   value=""
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">规格</label>
        <div class="layui-input-block">
            <input type="text" name="standards" id="standards" lay-verify="required" lay-reqtext="规格不能为空" placeholder=""
                   value=""
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">价格</label>
        <div class="layui-input-block">
            <input type="text" name="price" id="price" lay-verify="required" lay-reqtext="价格不能为空" placeholder=""
                   value=""
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">状态</label>
        <div class="layui-input-block">
            <input type="text" name="state" id="state" lay-verify="required" lay-reqtext="状态不能为空" placeholder=""
                   value=""
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</form>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$,
            //取出缓存中的车间id和设备类型id
            tid = layui.sessionData('id').tid,
            wosId = layui.sessionData('workshop').wosId;

        //查询所有车间并在下拉框中显示
        $.get("/workshop/show", function (data) {
            var content = "";
            var workshops = data.data
            for (var i = 0; i < workshops.length; i++) {
                var ws = workshops[i];
                console.log(wosId)
                if (ws.wosId === wosId) {
                    content += '<option id="' + ws.wosId + '" value="' + ws.wosId + '"selected>' + ws.wsname + '</option>'
                } else {
                    content += '<option id="' + ws.wosId + '" value="' + ws.wosId + '">' + ws.wsname + '</option>'
                }
            }
            $("#wosId").html(content);
            /*重新渲染页面*/
            form.render();
        });

        //查询所有设备类型并在下拉框中显示
        $.get("/equipmentType/show", function (data) {
            var content = "";
            var equipmentType = data.data
            for (var i = 0; i < equipmentType.length; i++) {
                var eq = equipmentType[i];
                if (eq.tid === tid) {
                    content += '<option id="' + eq.tid + '" value="' + eq.tid + '" selected>' + eq.ename + '</option>'
                } else {
                    content += '<option id="' + eq.tid + '" value="' + eq.tid + '">' + eq.ename + '</option>'
                }
            }
            $("#ename").html(content);
            /*重新渲染页面*/
            form.render();
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.post("/equipment/modify", data.field, function (data) {
                var index = layer.alert(data.msg, {
                    title: '消息'
                }, function () {
                    // 关闭弹出层
                    layer.close(index);
                    var iframeIndex = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(iframeIndex);
                });
            });
            //location.reload();
            return false;
        });

    });
</script>
</body>
</html>